import _path from 'path'
import _fs from 'fs-extra'
import _png from 'pngjs'
import _jpeg from 'jpeg-js'

/*
* @Author: gaara  
* @Date: 2020-03-02 10:27:22  
*/

export class ImgUtil {
    /**
     * 返回图片尺寸
     * @param path 图片路径
     */
    static getImgSize(path: string): { width: number, height: number } | undefined {
        let ext = _path.extname(path);
        let data = _fs.readFileSync(path);
        if (ext == '.png') {
            let png = _png.PNG.sync.read(data);
            return { width: png.width, height: png.height };
        }
        else if (ext == '.jpg') {
            var rawImageData = _jpeg.decode(data, true); // return as Uint8Array
            return { width: rawImageData.width, height: rawImageData.height };
        }
    }
}

